<?php

/******************************************************
 *					 	  DEBUG						  *
 ******************************************************/

/**
 * Prints variable info
 * @param mixed string $var The var to inform about
 * @param boolean $stop Stop script execution
 * @author Pablo
 * @since 1.0
 */
function pre($var, $stop= false){
	var_dump($var);
	if($stop) exit();	
}

/**
 * Get info about the function caller
 * @return stdClass
 * @author Pablo
 * @since 1.0
 */
function caller_info(){
	$call_info= debug_backtrace();
	array_shift($call_info);
	
	$info= new stdClass();
	$info->line= $call_info[0]['line'];
	$info->file= array_pop( explode('/', $call_info[0]['file']));
	$info->relative= str_replace(blg_get_config('site_path'), '', $info->file);
	$info->function= $call_info[0]['function'];	
	return $info;
}

/******************************************************
 *						BROWSER					  	  *
 ******************************************************/

/**
 * Returns properties of browser agent
 * @return stdClass
 * @author Pablo
 * @since 1.0
 */
function getBrowser() { 
    $u_agent = $_SERVER['HTTP_USER_AGENT']; 
    $bname = 'Unknown';
    $platform = 'Unknown';
	$version= "";
	
    //First get the platform?
     if (preg_match('/linux/i', $u_agent)) {
         $platform = 'linux';
     }
     elseif (preg_match('/macintosh|mac os x/i', $u_agent)) {
         $platform = 'mac';
     }
     elseif (preg_match('/windows|win32/i', $u_agent)) {
         $platform = 'windows';
     }
     
    // Next get the name of the useragent yes seperately and for good reason
     if(preg_match('/MSIE/i',$u_agent) && !preg_match('/Opera/i',$u_agent)) 
    { 
        $bname = 'Internet Explorer'; 
        $bname_short= 'IE';
        $ub = "MSIE"; 
    } 
    elseif(preg_match('/Firefox/i',$u_agent)) 
    { 
        $bname = 'Mozilla Firefox'; 
        $bname_short= 'F';
        $ub = "Firefox"; 
    } 
    elseif(preg_match('/Chrome/i',$u_agent)) 
    { 
        $bname = 'Google Chrome'; 
        $bname_short= 'C';
        $ub = "Chrome"; 
    } 
    elseif(preg_match('/Safari/i',$u_agent)) 
    { 
        $bname = 'Apple Safari'; 
        $bname_short= 'S';
        $ub = "Safari"; 
    } 
    elseif(preg_match('/Opera/i',$u_agent)) 
    { 
        $bname = 'Opera'; 
        $bname_short= 'O';
        $ub = "Opera"; 
    } 
    elseif(preg_match('/Netscape/i',$u_agent)) 
    { 
        $bname = 'Netscape'; 
        $bname_short= 'N';
        $ub = "Netscape"; 
    } 
    
    // finally get the correct version number
     $known = array('Version', $ub, 'other');
     $pattern = '#(?<browser>' . join('|', $known) .
     ')[/ ]+(?<version>[0-9.|a-zA-Z.]*)#';
     if (!preg_match_all($pattern, $u_agent, $matches)) {
         // we have no matching number just continue
     }
     
     
     
    // see how many we have
     $i = count($matches['browser']);
     if ($i != 1) {
         //we will have two since we are not using 'other' argument yet
         //see if version is before or after the name
         if (strripos($u_agent,"Version") < strripos($u_agent,$ub)){
             $version= $matches['version'][0];
         }
         else {
             $version= $matches['version'][1];
         }
     }
     else {
         $version= $matches['version'][0];
     }
     
    // check if we have a number
     if ($version==null || $version=="") {$version="?";}
     
     $info= new stdClass();
     $info->userAgent = $u_agent;
     $info->name = $bname;
     $info->short = $bname_short; 
     $info->version= $version;
     $info->info= $bname_short.floor($version);     
     $info->platform= $platform;
     $info->pattern= $pattern;
     return $info;
}

/******************************************************
 *					 	 STRINGS					  *
 ******************************************************/

/**
 * Add trainling slash to the end of a string if not present
 * @param string $str
 * @return string 
 * @author Pablo
 * @since 1.0
 */
//TODO Correct this function name to add_trailing_slash
function add_trainling_slash($str, $slash= '/'){
	if(strlen($str)>0 && substr($str, -1) !== $slash){
		return $str.$slash;
	}
	
	return $str;
}

/**
 * Compress CSS code lines
 * @param $buffer
 * @return $buffer compressed
 * @author Pablo
 * @since 1.0
 */
function compress_css($buffer){
	/* remove comments */
	$buffer = preg_replace("!/\*[^*]*\*+([^/][^*]*\*+)*/!", "", $buffer) ;
	/* remove tabs, spaces, newlines, etc. */
	$arr = array("\r\n", "\r", "\n", "\t", "  ", "    ", "    ") ;
	$buffer = str_replace($arr, "", $buffer) ;
	return $buffer;
}

function one_line_code($str){
	$str = ereg_replace("/\n\r|\r\n|\n|\r/", " ", $str);
	$str = preg_replace("/\t/", " ", $str);
	return preg_replace('/[^\S\n\r]+/', ' ', $str);	
}

/**
 * Generates a random string
 * @param $length
 * @param $char_collection
 * @author Pablo
 * @since 1.0
 */
function random_string($length, $char_collection=1){
	switch($char_collection){
		case 1: $chars= 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; break;
		case 2: $chars= 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'; break;
		case 3: $chars= '0123456789'; break;
		case 4: $chars= 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'; break;
		case 5: $chars= 'abcdefghijklmnopqrstuvwxyz'; break;
		case 6: $chars= 'abcdefghijklmnopqrstuvwxyz0123456789'; break;
		case 7: $chars= 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'; break;
		default: return FALSE; break;
	}
	return substr(str_shuffle(str_repeat($chars,$length)),0,$length);	
}

/**
 * Format a string to be printed in a HTML tag within double quotes
 * @param string $value
 * @return string
 * @author Pablo
 * @since 1.0
 */
function sanitize_for_html_attr($value){
	return htmlspecialchars($value, ENT_QUOTES, 'UTF-8');	
}

/**
 * Format a string to be printed in javascript code within single quotes
 * @param string $value
 * @return string
 * @author Pablo
 * @since 1.0
 */
function sanitize_for_js_attr($value){
	return htmlspecialchars($value, ENT_QUOTES, 'UTF-8');	
}

/******************************************************
 *					 	 SIZE						  *
 ******************************************************/

/**
 * Adjust dimension to set a max property
 * @param $width int
 * @param $height int
 * @param $max int
 * @param boolean $stretch (FALSE by default)
 * @return array($newwidth,$newheight)
 * @author Pablo
 * @since 1.0
 */
function blg_resize_size($width, $height, $max, $stretch= FALSE){
	$width= (float) $width;
	$height= (float) $height;
	$max= (float) $max;
	
	//MAKE IT SMALLER
	if($width>$max || $height>$max){
		if($width>$max){
			$relation= ($max/$width);
			$width= $width*$relation;
			$height= $height*$relation;
		}
	
		if($height>$max){
			$relation= ($max/$height);
			$width= $width*$relation;
			$height= $height*$relation;			
		}
		
		$width= round($width);
		$height= round($height);
	}
	//MAKE IT BIGGER
	elseif($stretch && $width<=$max && $height<=$max){
		if($width<=$max){
			$relation= ($max/$width);
			$width= $width*$relation;
			$height= $height*$relation;
		}
	
		if($height<=$max){
			$relation= ($max/$height);
			$width= $width*$relation;
			$height= $height*$relation;			
		}
		
		$width= round($width);
		$height= round($height);
		return blg_resize_size($width, $height, $max);		
	}
	
	return array((int)$width,(int)$height);
}











